<?php
/**
 * Setting Model
 */
namespace PUF\Plugin;

class Crud extends \PUF\Model {
	public static $useTable = 'puf_admin_cruds';
	
	protected static function createTable() {
		$sql = 'CREATE TABLE `' . self::$useTable . '` ( ';
		$sql .= ' `id` int(11) NOT NULL AUTO_INCREMENT, ';
		$sql .= ' `name` varchar(255) COLLATE utf8_bin NOT NULL, ';
		$sql .= ' `img` varchar(255) COLLATE utf8_bin NOT NULL, ';
		$sql .= ' `table` varchar(255) COLLATE utf8_bin NOT NULL, ';
		$sql .= ' `properties` text COLLATE utf8_bin NOT NULL, ';
		$sql .= ' PRIMARY KEY (`id`) ';
		$sql .= ' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; ';
		
		self::query($sql);
	}
	
	protected static function tableExists() {
		$sql = "SHOW TABLES LIKE '" . self::$useTable . "'";
		
		$result = self::query($sql);
		
		return $result->hasNext();
	}
	
	public static function find($options = []) {
		if (!self::tableExists()) {
			self::createTable();
		}
		
		return parent::find($options);
	}
}